package cn.dai.service;

import cn.dai.dao.UserDao;
import cn.dai.pojo.User;
import cn.dai.service.model.Page;

import java.util.List;

/**
 * @author ArkD42
 * @file RBAC
 * @create 2020 - 06 - 11 - 11:04
 */
public class UserServiceImpl implements UserService{

    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int addUser(User user) {
        return userDao.insertUser(user);
    }

    @Override
    public int delUser(Integer id) {
        return userDao.deleteUserBy(id);
    }

    @Override
    public int editUser(User user) {
        return userDao.updateUserById(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userDao.queryUserById(id);
    }

    @Override
    public User getUserByInfo(String name, String password) {
        return userDao.queryUserByInfo(name,password);
    }


    /**
     * 翻页Service
     * @param pageIndex
     * @return
     */
    @Override
    public Page<User> getUserPage(Integer pageIndex) {

        Page<User> userPage = new Page<>();

        userPage.setTotals( userDao.queryUserTotalCounts() );

        userPage.setPageSize(20);

        userPage.setPageIndex(pageIndex);

        userPage.setTotalPages( userPage.getPageSize() );

        userPage.setCurrentList(
                userDao.queryUserByLimit(
                        (userPage.getPageIndex() - 1) * userPage.getPageSize(),
                        userPage.getPageSize()
                )
        );

        return userPage;
    }

    @Override
    public int delUserByUidArray(String[] uidArray) {
        Integer[] uids = new Integer[uidArray.length];

        for (int i = 0; i < uids.length; i++) {
            uids[i] = Integer.valueOf(uidArray[i]);
        }

        return userDao.deleteUserByIds(uids);
    }

    @Override
    public List<User> getUsersBySearch(String keywords) {
        return userDao.queryUsersByLike(keywords);
    }
}

